import { FinalNode } from "../data/FinalNode";
import { ENodeData } from "../data/ENodeData";
type int = number;
//class FinalTree
    
    export  class FinalTree /* flash.display.Sprite */
    {
        constructor()
        {
            
            this._noteArray = [];
            this._linkArray = [];
            this.init();
            this.mouseEnabled = false;
            return;
        }

        protected init(): void
        {
            this._lineBmp = new Object /* flash.display.Bitmap */();
            this.addChild(this._lineBmp);
            this.createTree();
            this.initNotePos();
            this.drawLines();
            return;
        }

        protected createTree(): void
        {
            this.createChildNote(null);
            return;
        }

        protected createChildNote(arg1: FinalNode): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=0;
            var loc4=0;
            var loc5=null;
            var loc6=null;
            if (arg1) 
            {
                loc1 = new FinalNode();
                loc2 = 2 * this.NUM - arg1.num;
                if (arg1.num <= this.NUM) 
                {
                    return;
                }
                loc3 = 2 * this.NUM - 2 * loc2;
                loc4 = 2 * this.NUM - (2 * loc2 + 1);
                (loc5 = new FinalNode()).num = loc3;
                loc5.parent = arg1;
                loc5.depth = arg1.depth + 1;
                arg1.leftChild = loc5;
                (loc6 = new FinalNode()).num = loc4;
                loc6.parent = arg1;
                loc6.depth = arg1.depth + 1;
                arg1.rightChild = loc6;
                this._noteArray.push(loc5);
                this._noteArray.push(loc6);
                this.createChildNote(loc5);
                this.createChildNote(loc6);
            }
            else 
            {
                arg1 = new FinalNode();
                arg1.num = (2 * this.NUM - 1);
                arg1.depth = 0;
                this._rootNote = arg1;
                this._noteArray.push(arg1);
                this.createChildNote(arg1);
            }
            return;
        }

        protected resetNodes(): void
        {
            var loc2=null;
            var loc1=0;
            while (loc1 < this._noteArray.length) 
            {
                loc2 = this._noteArray[loc1] as FinalNode;
                loc2.data = ENodeData.DATANULL;
                loc2.arenaId = 0;
                ++loc1;
            }
            return;
        }

        protected initNotePos(): void
        {
            var loc1=0;
            var loc2=null;
            loc1 = 1;
            while (loc1 <= this.NUM) 
            {
                loc2 = this.getNoteAtNum(loc1);
                if (loc1 <= this.NUM / 2) 
                {
                    loc2.x = 0;
                    loc2.y = 30 * (loc1 - 1);
                }
                else 
                {
                    loc2.x = 546;
                    loc2.y = 30 * ((loc1 - 1) - this.NUM / 2);
                }
                ++loc1;
            }
            loc1 = this.NUM + 1;
            while (loc1 < 2 * this.NUM) 
            {
                loc2 = this.getNoteAtNum(loc1);
                if (loc2.leftChild.x < 250) 
                {
                    loc2.x = loc2.leftChild.x + 28;
                }
                else 
                {
                    loc2.x = loc2.leftChild.x - 28;
                }
                if (loc1 == (2 * this.NUM - 1)) 
                {
                    loc2.x = (loc2.leftChild.x + loc2.rightChild.x) / 2;
                }
                loc2.y = (loc2.leftChild.y + loc2.rightChild.y) / 2;
                ++loc1;
            }
            return;
        }

        protected getNoteAtNum(arg1: int): FinalNode
        {
            var loc1=0;
            while (loc1 < this._noteArray.length) 
            {
                if ((this._noteArray[loc1] as FinalNode).num == arg1) 
                {
                    return this._noteArray[loc1] as FinalNode;
                }
                ++loc1;
            }
            return null;
        }

        protected drawLines(): void
        {
            this._lineBmpd = new Object /* flash.display.BitmapData */(550, 485, true, 16777215);
            var loc1=0;
            while (loc1 < this._noteArray.length) 
            {
                this.drawLine(this._noteArray[loc1] as FinalNode, 4287401100);
                ++loc1;
            }
            this._lineBmp.bitmapData = this._lineBmpd;
            return;
        }

        protected drawLine(arg1: FinalNode, arg2: int=-16711936, arg3: int=1): void
        {
            var loc1=NaN;
            var loc2=NaN;
            var loc3=0;
            var loc4=0;
            var loc5=NaN;
            var loc6=NaN;
            if (arg1.parent) 
            {
                loc1 = Math.min(arg1.x, arg1.parent.x);
                loc2 = Math.max(arg1.x, arg1.parent.x);
                loc3 = 0;
                loc4 = 0;
                if (!arg1.parent.parent) 
                {
                    loc1 = Math.min(arg1.x, arg1.parent.x - 35 / 160 * (arg1.parent.x - arg1.x));
                    loc2 = Math.max(arg1.x, arg1.parent.x - 35 / 160 * (arg1.parent.x - arg1.x));
                }
                loc5 = Math.min(arg1.y, arg1.parent.y);
                loc6 = Math.max(arg1.y, arg1.parent.y);
                if (arg1.x < arg1.parent.x && arg1.y > arg1.parent.y) 
                {
                    loc3 = arg3;
                }
                if (arg1.x > arg1.parent.x && arg1.y < arg1.parent.y) 
                {
                    loc4 = arg3;
                }
                this._lineBmpd.fillRect(new Object /* flash.geom.Rectangle */(loc1, arg1.y, loc2 - loc1 + loc3, arg3), arg2);
                this._lineBmpd.fillRect(new Object /* flash.geom.Rectangle */(arg1.parent.x, loc5, arg3, loc6 - loc5 + loc4), arg2);
            }
            return;
        }

        protected /* const */NUM: int=32;

        protected /* var */_noteArray: Array<any>;

        protected /* var */_linkArray: Array<any>;

        protected /* var */_rootNote: FinalNode;

        protected /* var */_lineBmp: Object /* flash.display.Bitmap */;

        protected /* var */_lineBmpd: Object /* flash.display.BitmapData */;
    }
